package com.andatsoft.laisim.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.theme.Skin;
import com.andatsoft.laisim.theme.Theme;
import com.andatsoft.laisim.theme.ThemeManager;

public class LibraryGridAdapter extends BaseAdapter {
	private Theme mTheme;
	private Skin mSkin = ThemeManager.getSelectedSkin();

	private int[] names = new int[] { R.string.lib_grid_item_most_play,
			R.string.lib_grid_item_favorite, R.string.lib_grid_item_all_songs,
			R.string.lib_grid_item_album, R.string.lib_grid_item_artist,
			R.string.lib_grid_item_genre, R.string.lib_grid_item_playlist,
			R.string.lib_grid_item_folder, R.string.lib_grid_item_has_lyrics };
	private Drawable[] icons = new Drawable[] { mSkin.getLibScreenMostPlayed(),
			mSkin.getLibScreenFavorite(), mSkin.getLibScreenAllSong(),
			mSkin.getLibScreenAlbum(), mSkin.getLibScreenArtist(),
			mSkin.getLibScreenGenre(), mSkin.getLibScreenPlayList(),
			mSkin.getLibScreenFolder(), mSkin.getLibScreenHasLyrics() };
	private LayoutInflater mInflater;
	private int mSelectedItem = -1;

	public LibraryGridAdapter(Context context) {
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mTheme = ThemeManager.getSelectedTheme();
	}

	@SuppressLint("NewApi")
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder vHolder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.library_grid_item, parent,
					false);
			vHolder = new ViewHolder();
			// set value into textview
			TextView textView = (TextView) convertView
					.findViewById(R.id.tv_library_grid);
			vHolder.tvName = textView;
			ImageView img = (ImageView) convertView
					.findViewById(R.id.img_library_grid);
			vHolder.imgIcon = img;
			convertView.setTag(vHolder);

		} else {
			vHolder = (ViewHolder) convertView.getTag();
		}

		// Apply theme
		if (position == mSelectedItem) {

			vHolder.tvName.setTextColor(mTheme.getSelectedTextColor());
		} else {

			vHolder.tvName.setTextColor(mTheme.getNormalTextColor());
		}
		vHolder.tvName.setText(names[position]);
		vHolder.imgIcon.setImageDrawable(icons[position]);
		return convertView;
	}

	@Override
	public int getCount() {
		return names.length;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public void setSelectedItem(int item) {
		mSelectedItem = item;
	}

	class ViewHolder {
		ImageView imgIcon;
		TextView tvName;
	}

}
